/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package giuliani_mapelli_marocco.security;

import giuliani_mapelli_marocco.business.UserManager;
import giuliani_mapelli_marocco.entity.User;
import giuliani_mapelli_marocco.enumerations.InternalMessageType;
import javax.ejb.EJB;
import javax.inject.Named;
import javax.enterprise.context.RequestScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

@Named(value = "registrationBean")
@RequestScoped
public class RegistrationBean {

    @EJB
    private UserManager um;

    private User user;

    public RegistrationBean() {
    }

    public User getUser() {
        if (user == null) {
            user = new User();
        }
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    /**
     * registers the user
     * @return the next page to navigate to
     */
    public String register() {
        String resultString;
        resultString = um.save(user);
        if(resultString!=InternalMessageType.NO_ERRORS.getMessageContent()) {
            FacesContext context = FacesContext.getCurrentInstance();
            context.addMessage(null, new FacesMessage(resultString,  ""));
            return null;
        }
        return "user/home?faces-redirect=true";
    }

}
